#include<iostream>
using namespace std;
#include<iomanip>
#include<cmath>

const double x_node[5]={
	-0.906179845938664,
	-0.538469310105683,
	0,
	0.538469310105683,
	0.906179845938664};
const double omega[5]={
	0.236926885056189,
	0.478628670499366,
	0.568888888888889,
	0.478628670499366,
	0.236926885056189};

int n;
double GaussLegendre5(double a, double b, double (*func)(double, int)){
	double x, y=0;
	for(int i=0;i<5;i++){
		x = x_node[i] * (b-a)/2 + (a+b)/2 ;
		y += omega[i] * func(x, n);
	}
	y *= (b-a)/2;
	return y;
}

double f(double x,int n){
	return pow(x,n);
}

int main(){

	double a=0;
	double b=1;
	cout<<"Integration of f(x) from "<<a<<" to "<<b<<" is "<<endl;
	for(int i=1;i<=12;i++){
		cout<<"n="<<i<<"\t ";
		n=i;
		cout<<setprecision(15)<<GaussLegendre5(a,b,f);
		cout<<"\t "<< GaussLegendre5(a,b,f) - 1.0/(i+1)<<endl;
	}
	return 0;
}
